package com.lgh.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lgh.demo.MyApplication;
import com.lgh.demo.R;
import com.lgh.demo.bean.BeanGDUserInfo;
import com.lgh.demo.bean.BeanHistoryOrder;
import com.lgh.demo.bean.BeanLocationInfo;
import com.lgh.demo.util.ApiUtils;
import com.lgh.demo.util.Appconfig;
import com.lgh.demo.util.DialogUtils;
import com.lgh.demo.util.NetUtils;
import com.lgh.demo.util.ScreenUtil;
import com.lgh.demo.util.StrTool;
import com.lgh.demo.util.Tools;
import com.lgh.demo.util.WMConstantUtil;
import com.lgh.demo.util.ZXingUtils;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Log.e(TAG, "onCreate: " + ScreenUtil.getScreenWidth(this) + "x" + ScreenUtil.getScreenHeight(this));
        isFinishAfterSubmit = false;
        View waimai = findViewById(R.id.waimai);
        waimai.requestFocus();
        waimai.setOnClickListener(this);

        getGuangDongKeyNo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        queryLocationInfo();
    }

    public void turnToWaiMai(View view) {
        startActivity(new Intent(this, SellersAcitivity.class));
    }

    public void turnToHistory(View view) {
        startActivity(new Intent(this, HistoryActivity.class));
    }

    public void toMe(View view) {
        startActivity(new Intent(this, MeActivity.class));
    }

    public void clickTuiJian(View view) {
        startActivity(new Intent(this, DetailsActivity.class));
    }

    @Override
    public void onClick(View v) {
        startActivityForResult(new Intent(this, SellersAcitivity.class), REQUEST_CODE);
    }

    private void getGuangDongKeyNo() {
        String path = "http://gw.vodserver.local/GetConfig";
        NetUtils.getData(this, path, null, null, new NetUtils.NetCallBack() {
            @Override
            public void netBack(int requestTag, Object object) {
                if (object != null) {
                    String clientRgex = "client=\"(.*?)\"";
                    String caRegionCodeRgex = "caRegionCode=\"(.*?)\"";

                    String client = StrTool.getSubUtilSimple(object.toString(), clientRgex);
                    String caRegionCode = StrTool.getSubUtilSimple(object.toString(), caRegionCodeRgex);

                    Appconfig.setKeyNo(HomeActivity.this, client);
                    Appconfig.setRegionCode(HomeActivity.this, caRegionCode);

                    queryLocationInfo();
                }
            }
        });
    }


    private void queryLocationInfo() {
        String path = ApiUtils.getMyUrl(ApiUtils.URL_fetchUserInfo + Appconfig.getKeyNo(this));
        NetUtils.getData(this, path, null, BeanLocationInfo.class, new NetUtils.NetCallBack() {
            @Override
            public void netBack(int requestTag, Object object) {
                if (object != null) {
                    BeanLocationInfo beanLocationInfo = (BeanLocationInfo) object;
                    if (beanLocationInfo.getCode() == 0) {
                        MyApplication.beanLocationInfo = beanLocationInfo;
                        Tools.setStringPreference(HomeActivity.this,
                                WMConstantUtil.KEY_User_Location_Info, JSON.toJSONString(MyApplication.beanLocationInfo));
                    } else {
                        showUserInfoCode();
                    }
                } else {
                    showUserInfoCode();
                }
            }
        });
    }


    private void showUserInfoCode() {
        DialogUtils.showDialog(HomeActivity.this, ZXingUtils.createQRImage(ApiUtils.getMyUrl(ApiUtils.URL_USERINFO)
                + Appconfig.getKeyNo(HomeActivity.this), 500, 500));
    }

}
